
package com.toe.common.censor;


import com.toe.common.utils.Base64Util;
import com.toe.common.utils.FileUtil;
import com.toe.common.utils.HttpUtil;
import org.springframework.context.annotation.Configuration;

import java.net.URLEncoder;

/**
 * 图像审核接口
 */
@Configuration
public class ImgCensor {

    public static String ImgCensor(String ImgUrl) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            // 本地文件路径
            String filePath = ImgUrl;
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.c6dd4add3de69c7319bf0d024ac69cfd.2592000.1665834755.282335-27043482";
            String result = HttpUtil.post(url, accessToken, param);
            if (result.contains("data")){
                System.out.println("违规了!!!!");
                String[] msgs = result.split("msg");
                String[] split = msgs[1].split("\",\"");
                String[] split1 = split[0].split("\":\"");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String s = ImgCensor.ImgCensor("E:/临时文件夹/XHS_1663813441852010291016wc4xmqez9c0115jrz51udyiyw.jpg");
        System.out.println(s);
    }
}